#include "App_Monitor.h"
#include <stdlib.h>
#include <stdio.h>

#define Smoke_Density 1.5

extern uint8_t Alarm_Reset;

void App_Monitor_Init(void)
{
    Inf_ADC_Start();
}

char *App_Monitor_IsFire(void)
{
    float MQ_2 = Inf_ADC_MQ2Value();
    uint8_t isFire = MQ_2 / 2.5 * 100;
    char *Density = malloc(20);
    sprintf(Density, "烟雾浓度:%03d%%", isFire);
    if(MQ_2 > Smoke_Density || !HAL_GPIO_ReadPin(FIRE_GPIO_Port, FIRE_Pin))
    {
        // 疑似发生了火灾
        if(Alarm_Reset)
        {
            OLED_ClearArea(0, 32, 128, 16);
            OLED_ShowChinese(8, 32, "烟雾浓度：");
            OLED_ShowNum(88, 32, isFire, 3, OLED_8X16);
            OLED_ShowChar(112, 32, '%', OLED_8X16);
            return Density;
        }
        Inf_Buzzer_Sound(2000, 80, 50);
        Inf_LED_Blink(50, 5);
        OLED_ClearArea(0, 0, 128, 64);
        OLED_ShowChinese(16, 32, "发生火灾预警");
    }
    else
    {
        Alarm_Reset = 0;
        OLED_ClearArea(0, 32, 128, 16);
        OLED_ShowChinese(8, 32, "烟雾浓度：");
        OLED_ShowNum(88, 32, isFire, 3, OLED_8X16);
        OLED_ShowChar(112, 32, '%', OLED_8X16);
    }
    return Density;
}
